package com.lanyou.cook.web;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lanyou.cook.common.Constants;
import com.lanyou.cook.entity.domain.InterfaceVersionHistory;
import com.lanyou.cook.repository.InterfaceVersionHistoryRepository;
import com.lanyou.cook.service.security.UserSessionService;
import com.lanyou.cook.util.DateUtil;
import com.lanyou.cook.web.param.InterfaceVersionHistoryParam;
/**
 * 
 * @author 鏇炬棌鏍�
 *
 */
@RestController
@RequestMapping(Constants.BASE_URL_API + "/interfaceVersionHistories")
public class InterfaceVersionHistoryController {
	@Autowired
	UserSessionService userSessionService;
	@Autowired 
	private InterfaceVersionHistoryRepository interfaceVersionHistoryRepository;
	
	/**
	 * 鏌ヨ鍙傛暟鏉′欢涓嬬殑鎺ュ彛鐗堟湰鍘嗗彶鍜岄鍩熸帴鍙ｅ巻鍙蹭俊鎭拰澶辫触璀﹀憡淇℃伅
	 * @param param 绫诲瀷:InterfaceVersionHistoryParam
	 * @param pageable 绫诲瀷:Pageable
	 * @return 甯﹀弬鏁版潯浠剁殑鎺ュ彛鐗堟湰鍘嗗彶鍜岄鍩熸帴鍙ｅ巻鍙蹭俊鎭拰澶辫触璀﹀憡淇℃伅
	 */
	@RequestMapping("/search/findByExample")
	@RequiresPermissions(value={"interfaceVersionHistoryQuery:view","fieldInterfaceHistoryInfoQuery:view"}, logical=Logical.OR)
	public Page<InterfaceVersionHistory> findByExample(InterfaceVersionHistoryParam param, Pageable pageable) {
		
		Specification<InterfaceVersionHistory> spec = new Specification<InterfaceVersionHistory>() {
			@Override
			public Predicate toPredicate(Root<InterfaceVersionHistory> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicatesList = new ArrayList<>();
				if(StringUtils.isNotBlank(param.getInterfaceName())){
					predicatesList.add(cb.like(root.get("interfaceName"), '%' + param.getInterfaceName() + '%'));
					System.out.println(param.getInterfaceName());
				}
				if(StringUtils.isNoneBlank(param.getFieldName())){
					predicatesList.add(cb.equal(root.get("fieldName"),param.getFieldName()));
				}
				if(StringUtils.isNotBlank(param.getFieldSystemName())){
					predicatesList.add(cb.equal(root.get("fieldSystemName"), param.getFieldSystemName()));
				}else{
					Collection<String> fieldSystemIds = userSessionService.findFieldSystemIdsOfCurrentAuthenticatedUserCanManage();
					if(!CollectionUtils.isEmpty(fieldSystemIds)){
						predicatesList.add(root.get("systemId").in(fieldSystemIds));
					} else {
						predicatesList.add(cb.notEqual(cb.literal(1), 1));
					}
				}
			
				if(StringUtils.isNotBlank(param.getOperation())){
					predicatesList.add(cb.equal(root.get("operation"), param.getOperation()));
				}
				if(param.getChangeFromKettle()!=null){
					predicatesList.add(cb.equal(root.get("changeFromKettle"), param.getChangeFromKettle()));
				}
				
				
				if (param.getBeginOfCreateTime()==null) {
					param.setBeginOfCreateTime("1900-1-1 00:00:00");
				}
				
				DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				try {
					Date beginDate = sdf.parse(param.getBeginOfCreateTime());
					predicatesList.add(cb.greaterThan(root.get("createTime"), beginDate));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				if (param.getEndOfCreateTime()==null) {
					param.setEndOfCreateTime("2999-01-01 00:00:00");
				}
				
				try {
					Date endDate = sdf.parse(param.getEndOfCreateTime());
					predicatesList.add(cb.lessThan(root.get("createTime"), endDate));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			
				query.where(predicatesList.toArray(new Predicate[predicatesList.size()]));
				return null;
			}
		};
		
		return interfaceVersionHistoryRepository.findAll(spec, pageable);
	}
		
}
